package com.ruoyi.product.service.impl;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.product.domain.ProductSpec;
import com.ruoyi.product.domain.request.SpecItemGroup;
import com.ruoyi.product.domain.request.SpecTypeGroup;
import com.ruoyi.product.mapper.ProductSpecMapper;
import com.ruoyi.product.service.IProductSpecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.ruoyi.common.utils.SecurityUtils.getUsername;

/**
 * 商品规格属性Service业务层处理
 * 
 * @author liuhuizhong
 * @date 2025-08-12
 */
@Service
public class ProductSpecServiceImpl implements IProductSpecService
{
    @Autowired
    private ProductSpecMapper productSpecMapper;

    @Override
    public List<SpecTypeGroup> getSpecGroupData(Long spuId) {
        // 查询所有规格数据
        List<Map<String, Object>> specList = productSpecMapper.selectAllProductSpecs(spuId);

        // 按照 specType 分组
        Map<Integer, List<Map<String, Object>>> groupedByType = specList.stream()
                .collect(Collectors.groupingBy(spec -> ((Number) spec.get("specType")).intValue()));

        // 构造返回数据
        List<SpecTypeGroup> result = new ArrayList<>();

        for (Map.Entry<Integer, List<Map<String, Object>>> entry : groupedByType.entrySet()) {
            SpecTypeGroup typeGroup = new SpecTypeGroup();
            typeGroup.setSpecType(entry.getKey());

            List<SpecItemGroup> itemGroups = new ArrayList<>();
            for (Map<String, Object> spec : entry.getValue()) {
                SpecItemGroup itemGroup = new SpecItemGroup();
                itemGroup.setSpecName((String) spec.get("specName"));

                // 解析 values_json 字段
                String valuesJson = (String) spec.get("specValueGroup");
                if (valuesJson != null && !valuesJson.isEmpty()) {
                    try {
                        List<String> values = JSON.parseArray(valuesJson, String.class);
                        itemGroup.setSpecValueGroup(values);
                    } catch (Exception e) {
                        // 如果解析失败，设置为空列表
                        itemGroup.setSpecValueGroup(new ArrayList<>());
                    }
                } else {
                    itemGroup.setSpecValueGroup(new ArrayList<>());
                }

                itemGroups.add(itemGroup);
            }

            typeGroup.setSpecItemGroup(itemGroups);
            result.add(typeGroup);
        }

        // 按照 specType 排序
        result.sort(Comparator.comparing(SpecTypeGroup::getSpecType));

        return result;
    }

    /**
     * 查询商品规格属性
     * 
     * @param id 商品规格属性主键
     * @return 商品规格属性
     */
    @Override
    public ProductSpec selectProductSpecById(Long id)
    {
        return productSpecMapper.selectProductSpecById(id);
    }

    /**
     * 查询商品规格属性列表
     * 
     * @param productSpec 商品规格属性
     * @return 商品规格属性
     */
    @Override
    public List<ProductSpec> selectProductSpecList(ProductSpec productSpec)
    {
        return productSpecMapper.selectProductSpecList(productSpec);
    }

    /**
     * 新增商品规格属性
     * 
     * @param productSpec 商品规格属性
     * @return 结果
     */
    @Override
    public int insertProductSpec(ProductSpec productSpec)
    {
        productSpec.setCreateBy(getUsername());
        productSpec.setCreateTime(DateUtils.getNowDate());
        return productSpecMapper.insertProductSpec(productSpec);
    }

    /**
     * 修改商品规格属性
     * 
     * @param productSpec 商品规格属性
     * @return 结果
     */
    @Override
    public int updateProductSpec(ProductSpec productSpec)
    {
        productSpec.setUpdateBy(getUsername());
        productSpec.setUpdateTime(DateUtils.getNowDate());
        return productSpecMapper.updateProductSpec(productSpec);
    }

    /**
     * 批量删除商品规格属性
     * 
     * @param ids 需要删除的商品规格属性主键
     * @return 结果
     */
    @Override
    public int deleteProductSpecByIds(Long[] ids)
    {
        return productSpecMapper.deleteProductSpecByIds(ids);
    }

    /**
     * 删除商品规格属性信息
     * 
     * @param id 商品规格属性主键
     * @return 结果
     */
    @Override
    public int deleteProductSpecById(Long id)
    {
        return productSpecMapper.deleteProductSpecById(id);
    }
}
